package com.luyishi.service.impl;

import com.luyishi.dao.BookMapper;
import com.luyishi.entity.Book;
import com.luyishi.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Lenovo
 * @Date: 2020/9/1 22:45
 */
@Service
public class BookServiceImpl implements IBookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public int addBook(Book book) {
        return bookMapper.addBook(book);
    }

    @Override
    public List<Book> queryAllBook() {
        return bookMapper.queryAllBook();
    }

    @Override
    public Book queryBookById(int bId) {
        return bookMapper.queryBookById(bId);
    }

    @Override
    public List<Book> queryBookByName(String bookName) {
        return bookMapper.queryBookByName(bookName);
    }

    @Override
    public List<Book> queryBookByAuthor(String author) {
        return bookMapper.queryBookByAuthor(author);
    }

    @Override
    public Book updateBook(Book book) {
        Book afterBook = null;
        int row = bookMapper.updateBook(book);
        if (row > 0){
            afterBook = bookMapper.queryBookById(book.getBId());
        }
        return afterBook;
    }

    @Override
    public int deleteBook(int bId) {
        return bookMapper.deleteBook(bId);
    }

    @Override
    public int saleBook(int bId, int count){
        Book book = bookMapper.queryBookById(bId);
        int row = 0;
        int stock = book.getStock();
        if (stock >= count){
            book.setStock(stock - count);
            row = bookMapper.updateBookStock(book);
        }
        return row;
    }


}
